package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

public class Delete extends JInternalFrame {
	
	Connection con;
	int serial=0;
	DatabaseTest main;
	
	private JLabel lblNewLabel;
	private JTextField serialTF;
	private JButton button;
	private JLabel lblNewLabel_1;
	private JTextField codeTF;
	private JLabel lblNewLabel_2;
	private JTextField eaTF;
	private JButton btnNewButton;
	private JLabel kekka;
	
	public Delete(Connection con,DatabaseTest main) {
		super("삭제",true,true,true,true);
		addInternalFrameListener(new InternalFrameAdapter() {
			@Override
			public void internalFrameClosing(InternalFrameEvent e) {
				serialTF.setText("");
				codeTF.setText("");
				eaTF.setText("");
			}
		});
		this.con=con;
		this.main=main;
		setBounds(300, 300, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getSerialTF());
		getContentPane().add(getButton());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getCodeTF());
		getContentPane().add(getLblNewLabel_2());
		getContentPane().add(getEaTF());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getKekka());

	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("serial");
			lblNewLabel.setBounds(33, 22, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getSerialTF() {
		if (serialTF == null) {
			serialTF = new JTextField();
			serialTF.setBounds(116, 19, 116, 21);
			serialTF.setColumns(10);
		}
		return serialTF;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("조회");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						serial = Integer.parseInt(serialTF.getText());
						String sql = "select * from ptbl where serial=?";
						PreparedStatement psmt = con.prepareStatement(sql);
						psmt.setInt(1, serial);
						ResultSet rs = psmt.executeQuery();
						if(rs.next()){
							codeTF.setText(rs.getString(1));
							eaTF.setText(rs.getInt(2)+"");
							kekka.setText("serial번호 "+serial+"의 데이타가 조회되었습니다.");
						}else{
							kekka.setText("데이타가 없습니다");
							codeTF.setText("");
							eaTF.setText("");
							serialTF.requestFocus();
						}
						rs.close();
						psmt.close();
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			});
			button.setBounds(258, 18, 97, 23);
		}
		return button;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("code");
			lblNewLabel_1.setBounds(33, 58, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JTextField getCodeTF() {
		if (codeTF == null) {
			codeTF = new JTextField();
			codeTF.setEditable(false);
			codeTF.setBounds(116, 55, 116, 21);
			codeTF.setColumns(10);
		}
		return codeTF;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("ea");
			lblNewLabel_2.setBounds(33, 101, 57, 15);
		}
		return lblNewLabel_2;
	}
	private JTextField getEaTF() {
		if (eaTF == null) {
			eaTF = new JTextField();
			eaTF.setEditable(false);
			eaTF.setBounds(116, 98, 116, 21);
			eaTF.setColumns(10);
		}
		return eaTF;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("삭제");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						serial = Integer.parseInt(serialTF.getText());
						
						String sql = "delete from ptbl where serial=?";
						PreparedStatement psmt = con.prepareStatement(sql);
						psmt.setInt(1, serial);
						int r = psmt.executeUpdate();
						if(r==1){
							kekka.setText("serial 번호 "+serial+"의 데이타가 삭제되었습니다");
						}else{
							kekka.setText("삭제할 데이타가 없습니다");
						}
						codeTF.setText("");
						eaTF.setText("");
						if(main.select != null){
							main.select.setList();
						}
						psmt.close();
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			});
			btnNewButton.setBounds(258, 97, 97, 23);
		}
		return btnNewButton;
	}
	private JLabel getKekka() {
		if (kekka == null) {
			kekka = new JLabel("New label");
			kekka.setBounds(33, 176, 322, 15);
		}
		return kekka;
	}
}
